package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketCart;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.wx.WxCartCheck;
import com.cskaoyan.bean.wx.WxCartCheckoutVO;
import com.cskaoyan.bean.wx.WxCartData;
import com.cskaoyan.service.wx.WxCartService;
import com.cskaoyan.util.GetLogedAdmin;
import com.cskaoyan.util.GetLogedUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author Kongwo
 * @description
 * @since 2022/05/08 09:07
 */

@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    WxCartService wxCartService;

    @GetMapping("index")
    public BaseRespVo cartIndex() {

        Integer userId = GetLogedUser.getUserId();
        WxCartData wxCartData = wxCartService.getCartIndex(userId);
        return BaseRespVo.ok(wxCartData);
    }

    @PostMapping("add")
    public BaseRespVo cartAdd(@RequestBody MarketCart marketCart) {

        Integer userId = GetLogedUser.getUserId();
        if (userId == 0) {
            return new BaseRespVo("请登录",501);
        }

        marketCart.setUserId(userId);
        wxCartService.addCart(marketCart);
        return cartGoodscount();
    }

    @PostMapping("fastadd")
    public BaseRespVo cartFastAdd(@RequestBody MarketCart marketCart) {

        Integer userId = GetLogedUser.getUserId();
        if (userId == 0) {
            return new BaseRespVo("请登录",501);
        }

        return cartUpdate(marketCart);
    }

    @PostMapping("delete")
    public BaseRespVo cartDelete(@RequestBody WxCartCheck wxCartCheck) {

        Integer userId = GetLogedUser.getUserId();
        if (userId == 0) {
            return new BaseRespVo("请登录",501);
        }

        Integer[] productIds = wxCartCheck.getProductIds();
        WxCartData wxCartData = wxCartService.deleteCart(productIds, userId);
        return BaseRespVo.ok(wxCartData);
    }

    @PostMapping("update")
    public BaseRespVo cartUpdate(@RequestBody MarketCart marketCart) {
        Integer userId = GetLogedUser.getUserId();
        if (userId == 0) {
            return new BaseRespVo("请登录",501);
        }

        marketCart.setUserId(userId);
        MarketCart marketCart1 = wxCartService.updateCart(marketCart);
        return BaseRespVo.ok(marketCart1.getId());
    }

    @PostMapping("checked")
    public BaseRespVo cartChecked(@RequestBody WxCartCheck wxCartCheck) {
        Integer userId = GetLogedUser.getUserId();
        if (userId == 0) {
            return new BaseRespVo("请登录",501);
        }

        WxCartData wxCartData = wxCartService.updateCartChecked(wxCartCheck, userId);
        return BaseRespVo.ok(wxCartData);
    }

    @GetMapping("checkout")
    public BaseRespVo cartCheckout(Integer cartId, Integer addressId, Integer couponId, Integer userCouponId, Integer grouponRulesId, HttpSession session) {
        Integer userId = GetLogedUser.getUserId();
        if (userId == 0) {
            return new BaseRespVo("请登录",501);
        }

        WxCartCheckoutVO wxCartCheckoutVO = wxCartService.checkoutCart(cartId, addressId, couponId, userCouponId, grouponRulesId, userId);
        return BaseRespVo.ok(wxCartCheckoutVO);
    }

    @GetMapping("goodscount")
    public BaseRespVo cartGoodscount() {
        Integer userId = GetLogedUser.getUserId();
        Integer goodsCount = wxCartService.getCartIndex(userId).getCartTotal().getGoodsCount();
        return BaseRespVo.ok(goodsCount);
    }
}
